package com.zzc.study.controller;

import com.zzc.study.service.ChatAssistant;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class ChatLanguageModelController
{
    @Resource //原生低阶api调用
    private ChatLanguageModel chatLanguageModel;
    @Resource //我们自定义的接口，通过AiServices进行过封装，隔离了底层api细节
    private ChatAssistant chatAssistant;

    // http://localhost:9002/chatapi/hello
    @GetMapping(value = "/chatapi/hello")
    public String hello(@RequestParam(value = "prompt", defaultValue = "你是谁") String prompt)
    {
        String result = chatLanguageModel.chat(prompt);

        System.out.println("通过langchain4j调用模型返回结果："+result);

        return result;
    }

    //http://localhost:9002/chatapi/lowapi?prompt=茅台酒好喝吗
    @GetMapping(value = "/chatapi/lowapi")
    public String lowApi(@RequestParam("prompt") String prompt)
    {
        ChatResponse chatResponse = chatLanguageModel.chat(UserMessage.from(prompt));

        return chatResponse.aiMessage().text();
    }

    @GetMapping(value = "/chatapi/highapi")
    public String highApi(@RequestParam(value = "prompt", defaultValue = "你是谁") String prompt)
    {
        return chatAssistant.chat(prompt);
    }
}